#include "SWM341.h"
#include "user_app.h"
#include "dev_uart.h"
#include "ugui.h"
#include <stdio.h>
#include <stdlib.h>


uint16_t *LCD_Buffer = (uint16_t *)SDRAMM_BASE;



static uint8_t Get_match_index(uint16_t Command);
static void Get_bms_info(uint16_t cmd);
uint16_t Y_Modem_CRC(uint8_t *buf, uint16_t len);
static void BLE_CMD_Process(uint8_t *p_data, uint8_t get_lenth);
static void DMA_UART_BLUE_TX_Config(uint8_t *send_buffer, uint16_t send_len);
static void DMA_UART_TX_Config(uint8_t *send_buffer, uint16_t send_len);
static void send_BLE_GET_NAME_order(void);
static void send_BLE_GET_MAC_order(void);
bool FLASH_Erase_SIZE(uint32_t start_addr, uint32_t page_count);
void send_BLE_Stop_Scan_order(void);
void send_BLE_DISCON_order(void);
void eeprom_write(uint32_t addr, uint32_t data);
void config_read(void);
void send_BLE_Connect_order(def_ble_device_type ble_device);
void send_BLE_Scan_order(void);
void send_BLE_NAME_order(unsigned char *NAME_DATA);
unsigned int Getlen(const char *p1);
void check_name_len_write(uint16_t PACKET_SIZE);
int Instr(uint8_t *p1, unsigned int  len,  char *p2);
uint32_t bootload_data[300];
char str_temp[100];
int str3;
uint8_t auto_connect_time;
uint16_t get_crc, receive_crc;
/*

*********************************************************************************************************
    函数原型：unsigned int calccrc(unsigned char crcbuf,unsigned int crc)
    描    述：计算CRC的校验
    输入参数：unsigned char crcbuf,unsigned int crc
    输出参数：-
*********************************************************************************************************
*/
uint16_t calccrc(uint8_t crcbuf, uint16_t crc)
{
    uint8_t i;
    uint8_t tmp;
    crc = crc ^ crcbuf;
    for (i = 0; i < 8; i++)
    {
        tmp = crc & 1;
        crc = crc >> 1;
        if (tmp == 1)
            crc = crc ^ 0xa001;
    }
    return crc;
}

/*
*********************************************************************************************************
    函数原型：unsigned int CrcData(unsigned char* commdata,unsigned int len)
    描    述：CRC校验
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
uint16_t CrcData(uint8_t *commdata, uint16_t len)
{
    uint16_t  i;
    uint16_t  comcrc;
    comcrc = 0xffff;
    for (i = 0; i < len; i++)
    {
        comcrc = calccrc(commdata[i], comcrc);
    }
    return comcrc;
}

/*
*********************************************************************************************************
    函数原型：void UART_Receive(void)
    描    述：UART协议解析
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void Receive_process(UART_TYPE  data_p)
{
    uint16_t order, len, data_start, crc_cmd, PACKET_SIZE, i ;
    uint8_t data_temp[60],  ymod_cmd;
    uint32_t temp_h, temp_l;
    UART_TYPE uart_temp;
    char str[100];
    int string_num, str2;
    uart_temp = data_p;
    #if(BLUE_ENABLE)
    {
        if (s.ble_d)
        {
            s.ble_d = 0;
            // printf("get_data_blue len = %d\r\n", uart_temp.Lenth);
            //            for (int i = 0; i < uart_temp.Lenth; i++)
            //            {
            //                printf("%02X", uart_temp.Data[i]);
            //            }
            //DMA_UART_TX_Config(uart_temp.Data, uart_temp.Lenth);
            BLE_CMD_Process(uart_temp.Data, uart_temp.Lenth);
        }
    }
    #endif
    if (s.down_loading == 0)
    {
        if (uart_temp.Lenth == 8)
        {
            if ((uart_temp.Data[0] == 0x11)
                    && (uart_temp.Data[1] == 0x22)
                    && (uart_temp.Data[2] == 0x33)
                    && (uart_temp.Data[3] == 0x44)
                    && (uart_temp.Data[4] == 0x55)
                    && (uart_temp.Data[5] == 0x66)
                    && (uart_temp.Data[6] == 0x77)
                    && (uart_temp.Data[7] == 0x88)
               )
            {
                uart_debug_init(YMOD_BPS);
                s.down_loading = 1;
									s.erase_ok=0;
                eot_num = 0;
                receive_count = 0;
                wait_out_time = 60000;
            }
        }
    }
    else
    {
        s.crc_ok = 0;
        if (uart_temp.Lenth < 1030)
        {
            if (uart_temp.Lenth == 1)
            {
                s.crc_ok = 1;
                ymod_cmd = uart_temp.Data[0];
            }
            else if (uart_temp.Lenth >= 1025)
            {
                memcpy(&Y_1K_data, uart_temp.Data, uart_temp.Lenth);
                ymod_cmd = Y_1K_data.soh;
                get_crc = Y_Modem_CRC(&Y_1K_data.data, PACKET_SIZE_1K);
                receive_crc = (Y_1K_data.crcH << 8) + Y_1K_data.crcL;
                if (receive_crc == get_crc)
                {
                    s.crc_ok = 1;
                }
                PACKET_SIZE = PACKET_SIZE_1K;
            }
            else if (uart_temp.Lenth > 100)
            {
                memcpy(&Y_128_data, uart_temp.Data, uart_temp.Lenth);
                memcpy(&Y_1K_data, uart_temp.Data, uart_temp.Lenth);
                ymod_cmd = Y_128_data.soh;
                get_crc = Y_Modem_CRC(&Y_128_data.data, PACKET_SIZE_128);
                receive_crc = (Y_128_data.crcH << 8) + Y_128_data.crcL;
                if (receive_crc == get_crc)
                {
                    s.crc_ok = 1;
                }
                PACKET_SIZE = PACKET_SIZE_128;
            }
        }
        if (s.crc_ok)
        {
            switch (ymod_cmd)
            {
            case STX:
            {
                check_name_len_write(PACKET_SIZE);
               
                if (s.first_pack) 
                {
                    eot_num = 0;
                    Uart_send_data.Data[0] = ACK;
                    Uart_send_data.Data[1] = CRC16;
                    Uart_send_data.Lenth = 2;
                }
                else
                {
                    Uart_send_data.Data[0] = ACK;
                    Uart_send_data.Lenth = 1;
                }
								
								send_run_time = 0;
                s.send_run_flag = 0;
								DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                wait_out_time = 6000;
                receive_count = receive_count + 1;
                break;
            }
            case SOH:
            {
                check_name_len_write(PACKET_SIZE);
                send_run_time = 0;
                s.send_run_flag = 0;
                Uart_send_data.Data[0] = ACK;
                Uart_send_data.Lenth = 1;
                DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                receive_count = receive_count + 1;
                if (eot_num)
                {
                    s.down_loading = 0;
                    wait_out_time = 100;
                    //************写入正确固件标识
                    bootload_data[0] = CHECK_FLAG;
                    bootload_data[1] = 0xffffffFF;
                    bootload_data[2] = 0xffffffFF;
                    bootload_data[3] = 0xffffffFF;
                    FLASH_Write(Check_data_addr, bootload_data, 4);
                    //************清除需要下载固件标识
                    eeprom_write(31, 0x00);
                    receive_count = 0;
                    s.cmd_end = 0;
                    NVIC_SystemReset() ;
                }
                else
                {
                    if (s.first_pack)
                    {
                        eot_num = 0;
                        send_run_time = 0;
                        s.send_run_flag = 0;
                        Uart_send_data.Data[0] = CRC16;
                        Uart_send_data.Lenth = 1;
                        DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                    }
                }
                wait_out_time = 6000;
                break;
            }
            case EOT:
            {
                s.cmd_end = 1;
                if (eot_num == 0)
                {
                    eot_num++;
                    send_run_time = 0;
                    s.send_run_flag = 0;
                    Uart_send_data.Data[0] = NAK;
                    Uart_send_data.Lenth = 1;
                    DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                }
                else
                {
                    send_run_time = 0;
                    s.send_run_flag = 0;
                    Uart_send_data.Data[0] = ACK;
                    Uart_send_data.Data[1] = CRC16;
                    Uart_send_data.Lenth = 2;
                    DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                }
                wait_out_time = 6000;
                break;
            }
            }
        }
        else
        {
            if (uart_temp.Lenth > 128)
            {
                send_run_time = 0;
                s.send_run_flag = 0;
                Uart_send_data.Data[0] = NAK;
                Uart_send_data.Lenth = 1;
                DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
            }
        }
    }
}
//***串口信息解析
void UART_Receive(void)
{
    if (s.uart0_receive_flag)
    {
        s.uart0_receive_flag = 0;
        s.ble_d = 0;
        Receive_process(uart_data0);
    }
    if (s.uart_blue_receive_flag)
    {
        s.uart_blue_receive_flag = 0;
        s.ble_d = 1;
        Receive_process(uart_data_blue);
    }
}

static void BLE_CMD_Process(uint8_t *p_data, uint8_t len)
{
    unsigned int ok, get_lenth = 0, i_num = 0, do_len, y_lop;
    int string_num, str2;
    char str[100];
    uint8_t speed_c, lop, i_lop;
    get_lenth = len;
    //**********************************************************
    if ((p_data[0] == 0x54) && (p_data[1] == 0x54) && (p_data[2] == 0x4D))
    {
        if (get_lenth > 10)
        {
        }
    }
    else
    {
        uint8_t  str_len = 0;
    }
}

void DMA_CH_Open2(uint32_t chn)
{
    DMA->CH[chn].CR |= (1 << DMA_CR_TXEN_Pos);
}

static void DMA_UART_TX_Config(uint8_t *send_buffer, uint16_t send_len)
{
    s.send_tx_dma = 1;
    DE_485_TX();//切换到发送状态
    systick_delay_ms(1);//延时1ms后再发送数据
    uart_send_msg(UART0, send_buffer, send_len);
    systick_delay_ms(1);//延时1ms后再发送数据

    DE_485_RX();//切换到发送状态

    s.send_tx_dma = 0;
	
}
static void DMA_UART_BLUE_TX_Config(uint8_t *send_buffer, uint16_t send_len)
{
    DMA_InitStructure DMA_initStruct;
    //    s.send_tx_dma = 1;
    //    send_tx_len = send_len / 5;
    //    send_tx_time = 0;
    //   printf("uart_send data_blue len = %d\r\n", send_len);
    //            for (int i = 0; i < send_len; i++)
    //            {
    //                printf("%c", send_buffer[i]);
    //            }
    DMA_initStruct.Mode = DMA_MODE_SINGLE;
    DMA_initStruct.Unit = DMA_UNIT_BYTE;
    DMA_initStruct.Count = send_len;
    DMA_initStruct.SrcAddr = (uint32_t)send_buffer;
    DMA_initStruct.SrcAddrInc = 1;
    DMA_initStruct.DstAddr = (uint32_t)&UART2->DATA;
    DMA_initStruct.DstAddrInc = 0;
    DMA_initStruct.Handshake = DMA_CH2_UART2TX;
    DMA_initStruct.Priority = DMA_PRI_LOW;
    DMA_initStruct.INTEn = DMA_IT_DONE;
    DMA_CH_Init(DMA_CH2, &DMA_initStruct);
    DMA_CH_INTClr(DMA_CH2, DMA_IT_DONE);
    DMA_CH_Open(DMA_CH2);
}

void isr_uart_dma_tx(void)
{
    if (DMA_CH_INTStat(DMA_CH0, DMA_IT_DONE))
    {
        DMA_CH_INTClr(DMA_CH0, DMA_IT_DONE);
        if (s.send_tx_dma)
        {
            // s.send_tx_dma    = 0;
            // DE_485_RX();//发送完成后 切换为接收状态 暂时不加延时
        }
    }
    if (DMA_CH_INTStat(DMA_CH2, DMA_IT_DONE))
    {
        DMA_CH_INTClr(DMA_CH2, DMA_IT_DONE);
        s.send_blue_tx_dma = 0;
    }
}
/*
*********************************************************************************************************
    函数原型：void UART_Send(void)
        描    述：UART数据发送
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void UART_Send(void)
{
    uint8_t get_send_index, i;
    if (s.send_run_flag) //300ms 触发一次
    {
        s.send_run_flag = 0;
        Uart_send_data.Data[0] = 0x43;
        Uart_send_data.Lenth = 1;
        DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
    }
}



/*
*********************************************************************************************************
    函数原型：void led_debug_init(void)
        描    述：指示灯端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void led_debug_init(void)
{
    GPIO_Init(GPIOM, PIN2, 1, 1, 0, 0);
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void LED_ON(void)
{
    GPIO_AtomicSetBit(GPIOM, PIN2); /* 点亮LED光 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void LED_OFF(void)
{
    GPIO_AtomicClrBit(GPIOM, PIN2); /* 熄灭LED光 */
}
/*
*********************************************************************************************************
    函数原型：void buzzer_init(void)
        描    述：蜂鸣器端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void buzzer_init(void)
{
    PORT_Init(PORTD, PIN2, PORTD_PIN2_GPIO, 0);
    GPIO_Init(GPIOD, PIN2, 1, 1, 0, 0);
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void BUZZER_ON(void)
{
    GPIO_AtomicSetBit(GPIOD, PIN2); /* 开启蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void BUZZER_OFF(void)
{
    GPIO_AtomicClrBit(GPIOD, PIN2); /* 关闭蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void buzzer_init(void)
        描    述：蜂鸣器端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void RF_init(void)
{
    PORT_Init(PORTA, PIN8, PORTA_PIN8_GPIO, 0);
    GPIO_Init(GPIOA, PIN8, 1, 1, 0, 0);
    PORT_Init(PORTD, PIN1, PORTD_PIN1_GPIO, 0);
    GPIO_Init(GPIOD, PIN1, 0, 1, 0, 0);//BLZ 信号，低电平有效
    #if BLUE_ENABLE
    uart_blue_init();
    RF_OFF();
    #endif
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void RF_ON(void)
{
    GPIO_AtomicSetBit(GPIOA, PIN8); /* 开启蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void RF_OFF(void)
{
    GPIO_AtomicClrBit(GPIOA, PIN8); /* 关闭蜂鸣器 */
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void DE_485_TX(void)
{
    GPIO_AtomicSetBit(GPIOM, PIN3); /* 发送状态 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void DE_485_RX(void)
{
    GPIO_AtomicClrBit(GPIOM, PIN3); /* 接收状态 */
}

void Buzzer_cotrl(unsigned int per_time)
{
    if (per_time)
    {
        BUZZER_ON();
        s.buzzer = 1;
        buzzer_time = 0;
        buzzer_set_time = per_time;
    }
}



unsigned int Getlen(const char *p1)
{
    unsigned int  lop;
    for (lop = 0; (* (p1 + lop) != 0) && (lop < 255); lop++);
    return lop;
}
int Instr(uint8_t *p1, unsigned int  len,  char *p2)
{
    unsigned int  lop = 0, y;
    unsigned int  str1_lenth, str2_lenth;
    str1_lenth = Getlen(p1);
    str2_lenth = Getlen(p2);
    if (len > str2_lenth)
    {
        len = str2_lenth;
    }
    if (str1_lenth < len)
    {
        return -1;
    }
    for (lop = 0; lop < str1_lenth; lop++)
    {
        if (*(p1 + lop) == *p2)
        {
            for (y = 0; y < len; y++)
            {
                if (*(p1 + lop + y) != *(p2 + y))
                {
                    break;
                }
            }
            if (y == len)
            {
                return lop;
            }
        }
    }
    return -1;
}
//***********************************************************
void send_BLE_Reset_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        len = Getlen(BLE_RESET);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = BLE_RESET[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Scan_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        char *order = "TTM:SCA-?";
        len = Getlen(order);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = order[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Stop_Scan_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        char *order = "TTM:DISCA-";
        len = Getlen(order);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = order[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Connect_order(def_ble_device_type ble_device)
{
    #if(BLUE_ENABLE)
    {


        ;
    }
    #endif
}
static void send_BLE_GET_NAME_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, len;
    uint8_t ble_send_data[30];
    len = Getlen(BLE_GET_NAME);
    //len = 9;
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = BLE_GET_NAME[i];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    //uart_send_msg(UART2,ble_send_data,len);
    #endif
}
static void send_BLE_GET_MAC_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, len;
    uint8_t ble_send_data[30];
    len = Getlen(GET_MAC);
    //len = 9;
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = GET_MAC[i];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    //uart_send_msg(UART2,ble_send_data,len);
    #endif
}
//*******蓝牙广播头信息更改*************************************************
void send_BLE_ADD_order(unsigned char *MAC_DATA)
{
    #if(BLUE_ENABLE)
    #endif
}
//*******蓝牙名称更改*************************************************
void send_BLE_NAME_order(unsigned char *DEF_NAME)
{
    #if(BLUE_ENABLE)
    uint8_t i, j, len, len2;
    uint8_t ble_send_data[40];
    len = Getlen(BLE_SET_NAME);
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = BLE_SET_NAME[i];
    }
    len2 = Getlen(DEF_NAME);
    for (j = 0; j < len2; j++)
    {
        ble_send_data[i + j] = DEF_NAME[j];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len + len2);
    #endif
}
//*******蓝牙断开*************************************************
void send_BLE_DISCON_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, j, len, len2;
    uint8_t ble_send_data[40];
    char *order = "TTM:DISCON-";
    len = Getlen(order);
    for (j = 0; j < len; j++)
    {
        ble_send_data[i + j] = order[j];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    #endif
}


#define EEPROM_ADDR   0x7D000

uint32_t WrBuff[32] = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
                      };

uint32_t eeprom_read_byte(uint32_t addr)
{
    uint32_t p = *((volatile uint32_t *)(EEPROM_ADDR + addr * 4));
    return p;
}
void eeprom_write(uint32_t addr, uint32_t data)
{
    uint16_t lop;
    for (lop = 0; lop < 32; lop++)
    {
        WrBuff[lop] = *((volatile uint32_t *)(EEPROM_ADDR + lop * 4));
    }
    FLASH_Erase(EEPROM_ADDR);
    WrBuff[addr] = data;
    FLASH_Write(EEPROM_ADDR, WrBuff, 32);
}


bool FLASH_Erase_SIZE(uint32_t start_addr, uint32_t page_count)
{
    uint16_t lop;
    for (lop = 0; lop < page_count; lop++)
    {
        if (FLASH_RES_OK != FLASH_Erase(start_addr + lop * Flash_Alignment))
        {
            return false;
        }
    }
    return true;
}
void config_read(void)
{
    uint8_t name_lenth, lop;
    s.blue_connect = 0;
    s.scan_start_flag = 0;
    s.scaning_flag = 0;
    s.connect_start = 0;
    wait_out_time = 0;
    s.ble_off_line = 0;
    //************************************************
    if (eeprom_read_byte(31) == 0xaa)
    {
        s.wait_load = 1;
    }
    else
    {
        s.wait_load = 0;
    }
}



uint16_t Y_Modem_CRC(uint8_t *buf, uint16_t len)
{
    uint16_t chsum;
    uint16_t stat;
    uint16_t i;
    uint8_t *in_ptr;
    in_ptr = buf;
    chsum = 0;
    for (stat = len ; stat > 0; stat--) //len????????
    {
        chsum = chsum ^ (uint16_t)(*in_ptr++) << 8;
        for (i = 8; i != 0; i--)
        {
            if (chsum & 0x8000)
            {
                chsum = chsum << 1 ^ 0x1021;
            }
            else
            {
                chsum = chsum << 1;
            }
        }
    }
    return chsum;
}
void check_name_len_write(uint16_t PACKET_SIZE)
{
    uint16_t i;
    int string_num, str2;
    char str[100];
    if ((Y_1K_data.block_num == 0) && (s.erase_ok == 0))
    {
        string_num = 3;
        while ((Y_1K_data.data[string_num] != 0x00) && (string_num < 100))
        {
            string_num++;
        }
        strncpy(get_file.filename, &Y_1K_data.data[0], string_num);
        str2 = string_num;
        while ((Y_1K_data.data[str2] != 0x20) && (str2 < 100))
        {
            str2++;
        }
        strncpy(str, &Y_1K_data.data[1 + string_num], str2 - string_num);
        get_file.filelenth = atoi(str);
				
		
        if (true == FLASH_Erase_SIZE(Firmware_Addr, APP_Flash_page_Count))
        {
            s.erase_ok = 1;
				  	s.first_pack=1;
            s.cmd_end = 0;
            write_len = 0;
            write_addr = Firmware_Addr;
						receive_count=0;
        }
    }
    else
    {
				s.first_pack=0;
        if (s.cmd_end == 0)
        {
            for (i = 0; i < PACKET_SIZE / 4; i++)
            {
                bootload_data[i] = Y_1K_data.data[i * 4 + 0] + (Y_1K_data.data[ i * 4 + 1] << 8) + (Y_1K_data.data[ i * 4 + 2] << 16) + (Y_1K_data.data[  i * 4 + 3] << 24);
                write_len = write_len + 4;
                if (write_len > get_file.filelenth)
                {
                    bootload_data[i] = 0xffffffFF;
                }
            }
            FLASH_Write(write_addr, bootload_data, PACKET_SIZE / 4);
            write_addr = write_addr + PACKET_SIZE;
        }
    }
}



void RGBLCDInit(void)
{
}

void MemoryInit(void)
{
    SDRAM_InitStructure SDRAM_InitStruct;
    PORT_Init(PORTM, PIN13, PORTM_PIN13_SDR_CLK, 0);
    PORT_Init(PORTM, PIN14, PORTM_PIN14_SDR_CKE, 0);
    PORT_Init(PORTB, PIN7, PORTB_PIN7_SDR_WE, 0);
    PORT_Init(PORTB, PIN8, PORTB_PIN8_SDR_CAS, 0);
    PORT_Init(PORTB, PIN9, PORTB_PIN9_SDR_RAS, 0);
    PORT_Init(PORTB, PIN10, PORTB_PIN10_SDR_CS, 0);
    PORT_Init(PORTE, PIN15, PORTE_PIN15_SDR_BA0, 0);
    PORT_Init(PORTE, PIN14, PORTE_PIN14_SDR_BA1, 0);
    PORT_Init(PORTN, PIN14, PORTN_PIN14_SDR_A0, 0);
    PORT_Init(PORTN, PIN13, PORTN_PIN13_SDR_A1, 0);
    PORT_Init(PORTN, PIN12, PORTN_PIN12_SDR_A2, 0);
    PORT_Init(PORTN, PIN11, PORTN_PIN11_SDR_A3, 0);
    PORT_Init(PORTN, PIN10, PORTN_PIN10_SDR_A4, 0);
    PORT_Init(PORTN, PIN9, PORTN_PIN9_SDR_A5, 0);
    PORT_Init(PORTN, PIN8, PORTN_PIN8_SDR_A6, 0);
    PORT_Init(PORTN, PIN7, PORTN_PIN7_SDR_A7, 0);
    PORT_Init(PORTN, PIN6, PORTN_PIN6_SDR_A8, 0);
    PORT_Init(PORTN, PIN3, PORTN_PIN3_SDR_A9, 0);
    PORT_Init(PORTN, PIN15, PORTN_PIN15_SDR_A10, 0);
    PORT_Init(PORTN, PIN2, PORTN_PIN2_SDR_A11, 0);
    PORT_Init(PORTM, PIN15, PORTM_PIN15_SDR_A12, 0);
    PORT_Init(PORTE, PIN7, PORTE_PIN7_SDR_D0, 1);
    PORT_Init(PORTE, PIN6, PORTE_PIN6_SDR_D1, 1);
    PORT_Init(PORTE, PIN5, PORTE_PIN5_SDR_D2, 1);
    PORT_Init(PORTE, PIN4, PORTE_PIN4_SDR_D3, 1);
    PORT_Init(PORTE, PIN3, PORTE_PIN3_SDR_D4, 1);
    PORT_Init(PORTE, PIN2, PORTE_PIN2_SDR_D5, 1);
    PORT_Init(PORTE, PIN1, PORTE_PIN1_SDR_D6, 1);
    PORT_Init(PORTE, PIN0, PORTE_PIN0_SDR_D7, 1);
    PORT_Init(PORTE, PIN8, PORTE_PIN8_SDR_D8, 1);
    PORT_Init(PORTE, PIN9, PORTE_PIN9_SDR_D9, 1);
    PORT_Init(PORTE, PIN10, PORTE_PIN10_SDR_D10, 1);
    PORT_Init(PORTE, PIN11, PORTE_PIN11_SDR_D11, 1);
    PORT_Init(PORTE, PIN12, PORTE_PIN12_SDR_D12, 1);
    PORT_Init(PORTE, PIN13, PORTE_PIN13_SDR_D13, 1);
    PORT_Init(PORTC, PIN14, PORTC_PIN14_SDR_D14, 1);
    PORT_Init(PORTC, PIN15, PORTC_PIN15_SDR_D15, 1);
    PORT_Init(PORTB, PIN6, PORTB_PIN6_SDR_LDQM, 0);
    PORT_Init(PORTM, PIN12, PORTM_PIN12_SDR_UDQM, 0);
    SDRAM_InitStruct.Size = SDRAM_SIZE_8MB;
    SDRAM_InitStruct.ClkDiv = SDRAM_CLKDIV_1;
    SDRAM_InitStruct.CASLatency = SDRAM_CASLATENCY_3;
    SDRAM_InitStruct.TimeTRP  = SDRAM_TRP_2;
    SDRAM_InitStruct.TimeTRCD = SDRAM_TRCD_2;
    SDRAM_InitStruct.TimeTRFC  = SDRAM_TRFC_9;
    SDRAM_Init(&SDRAM_InitStruct);
}
